﻿using System.Windows;
using System.Windows.Controls;
using Utils.Wpf;

namespace StockAnalyst2.Plugins.Visualization.Historical
{
	internal partial class CustomDataViewer : UserControl
	{
		#region Properties

		public new CustomDataViewerVM DataContext
		{
			get
			{
				return (CustomDataViewerVM)base.DataContext;
			}
			set
			{
				if (value != this.DataContext)
				{
					if (this.DataContext != null)
					{
						this.DataContext.SaveFileDialogRequested -= DataContext_SaveFileDialogRequested;
					}

					base.DataContext = value;

					if (this.DataContext != null)
					{
						this.DataContext.SaveFileDialogRequested += DataContext_SaveFileDialogRequested; // Disposed
					}
				}
			}
		}

		#endregion

		#region Public Methods

		public CustomDataViewer()
		{
			InitializeComponent();
		}

		#endregion

		#region Event Handlers

		private void DataContext_SaveFileDialogRequested(object sender, SaveFileDialogRequestedEventArgs e)
		{
			e.DialogResult = e.Dialog.ShowDialog(Window.GetWindow(this));
		}

		#endregion

		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{
			// Hack because the datacontext is set thru XAML and thus the new defined Datacontext property it's not called
			if (this.DataContext != null)
			{
				this.DataContext.SaveFileDialogRequested += DataContext_SaveFileDialogRequested; // Disposed
			}
		}
	}
}
